package com.qin.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.common.api.MyBatisPlusPage;
import com.qin.mall.domain.OmsOrderReturnReason;
import com.qin.mall.service.OmsOrderReturnReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 订单原因管理
 * @author Leisure
 * create 2022 2022/11/11 8:45
 **/
@RestController
@RequestMapping("/Reason")
public class OmsOrderReturnReasonController {
    
    @Autowired
    private OmsOrderReturnReasonService orderReturnReasonService;

    /**
     *添加订单原因
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody OmsOrderReturnReason returnReason){
        int count = orderReturnReasonService.create(returnReason);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 修改退货原因
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id,
                         @RequestBody OmsOrderReturnReason returnReason){
        int count = orderReturnReasonService.update(id, returnReason);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 批量删除退货原因
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam("ids") List<Long> ids){
        int count = orderReturnReasonService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();

    }

    /**
     * 分页获取退货原因
     */
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<MyBatisPlusPage<OmsOrderReturnReason>> list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page<OmsOrderReturnReason> list = orderReturnReasonService.getlist(pageNum, pageSize);
        return CommonResult.success(MyBatisPlusPage.mpPage(list));

    }

    /**
     * 批量修改退货原因状态
     */
    @PostMapping("/update/status")
    public CommonResult updateStatus(@RequestParam("status") Integer status,
                               @RequestParam("ids") List<Long> ids){
        int count = orderReturnReasonService.updateStatus(ids, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取单个退货原因详情信息
     */
    @GetMapping("/{id}")
    public CommonResult<OmsOrderReturnReason> getItem(@PathVariable Long id){
        OmsOrderReturnReason item = orderReturnReasonService.getItem(id);
        return CommonResult.success(item);
    }

}
